package com.qf.java2106.huangshengrui.hotel.controller.login;

import com.alibaba.fastjson.JSON;
import com.qf.java2106.huangshengrui.hotel.common.ResultVO;
import com.qf.java2106.huangshengrui.hotel.constant.MethodConstant;
import com.qf.java2106.huangshengrui.hotel.controller.BaseServlet;
import com.qf.java2106.huangshengrui.hotel.factory.BeanFactory;
import com.qf.java2106.huangshengrui.hotel.pojos.User;
import com.qf.java2106.huangshengrui.hotel.service.IUserService;
import com.qf.java2106.huangshengrui.hotel.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @author q
 * @version V1.0
 * @date 2021/10/28
 **/
@WebServlet("/login")
public class LoginController extends BaseServlet {

    IUserService userService = (IUserService) BeanFactory.getBean("userService");

    /**
     * 登陆
     * @param request
     * @param response
     */
    public String login(HttpServletRequest request, HttpServletResponse response) throws IOException, InvocationTargetException, IllegalAccessException {

        String check = request.getParameter("check");
        String sessionCode = (String) request.getSession().getAttribute("sessionCode");
        if (!(null!=sessionCode && sessionCode.equals(check))){
            return "<script>alert('验证码错误~');location.href='"+ request.getContextPath() +"/';</script>";
        }

        Map<String, String[]> parameterMap = request.getParameterMap();
        User loginUser = new User();
        BeanUtils.populate(loginUser,parameterMap);
        ResultVO vo = userService.userLogin(loginUser);
        if (vo.getSuccess()){
            request.getSession().setAttribute("username", loginUser.getUsername());
            request.getSession().setAttribute("userIsAdmin", loginUser.getIsAdmin());
            if (loginUser.getIsAdmin()==1){
                return MethodConstant.REDIRECT_PREFIX + request.getContextPath() + "/backend/";
            } else {
                return MethodConstant.REDIRECT_PREFIX + request.getContextPath() + "/front/";
            }
        }
        return"<script>alert('" + vo.getMessage() +"');location.href='"+ request.getContextPath() +"/';</script>";
    }

    /**
     * 注册
     * @param request
     * @param response
     */
    public String logon(HttpServletRequest request, HttpServletResponse response) throws IOException, InvocationTargetException, IllegalAccessException {

        String check = request.getParameter("check");
        String sessionCode = (String) request.getSession().getAttribute("sessionCode");
        if (!(null!=sessionCode && sessionCode.equals(check))){
            return "<script>alert('验证码错误~');location.href='"+ request.getContextPath() +"/logon.jsp';</script>";
        }

        Map<String, String[]> parameterMap = request.getParameterMap();
        User logonUser = new User();
        BeanUtils.populate(logonUser,parameterMap);
        ResultVO vo = userService.userLogon(logonUser);
        if (vo.getSuccess()){
            return"<script>alert('" + vo.getMessage() +"');location.href='"+ request.getContextPath() +"/';</script>";
        }
        return"<script>alert('" + vo.getMessage() +"');location.href='"+ request.getContextPath() +"/logon';</script>";
    }

    /**
     * 检查用户名
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    public String nameExists(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userName = request.getParameter("userName");
        ResultVO vo = userService.userCheck(userName);
        return JSON.toJSONString(vo);
    }

    /**
     * 检查用户名
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    public String nameLogonExists(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userName = request.getParameter("userName");
        ResultVO vo = userService.userLogonCheck(userName);
        return JSON.toJSONString(vo);
    }

    /**
     * 退出登陆
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    public String logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getSession().invalidate();
        return MethodConstant.REDIRECT_PREFIX + request.getContextPath()+"/";
    }

}
